// cmd/server/main.go
package main

import (
	// _ "gdrive/docs" // Swagger

	"gdrive/config"
	"gdrive/internal/handler"
	"gdrive/internal/service"
	"gdrive/pkg/storage"
	"log"

	"github.com/gin-gonic/gin"
	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
)

func main() {
	// 初始化数据库
	db, err := sqlx.Connect("postgres", "user=gdrive password=secret123 dbname=gdrive host=localhost sslmode=disable")
	if err != nil {
		log.Fatal("无法连接数据库:", err)
	}

	cfg := config.LoadConfig()
	log.Println(cfg)
	runMigrations(cfg)

	// 初始化 MinIO
	minioClient, err := storage.NewMinIOStorage("localhost:9000", "minioadmin", "minioadmin", "gdrive-files")
	if err != nil {
		log.Fatal("无法连接 MinIO:", err)
	}

	// 初始化服务
	fileHandler := &handler.FileHandler{
		Service: service.NewFileService(db, minioClient),
	}

	r := gin.Default()

	// 中间件
	r.Use(authMiddleware()) // JWT 验证

	// 路由
	api := r.Group("/api/v1")
	{
		api.POST("/upload", fileHandler.UploadFile)
		api.GET("/files", fileHandler.ListFiles)
		// 其他路由...
	}

	log.Println("服务器启动在 :8080")
	r.Run(":8080")
}
